package com.it.server.controller.admin;

import cn.hutool.core.date.DateTime;
import com.it.common.result.Result;
import com.it.pojo.DTO.MerchantDTO;
import com.it.pojo.Entity.Dish;
import com.it.pojo.Entity.Merchant;
import com.it.pojo.Entity.Orders;
import com.it.server.service.DishService;
import com.it.server.service.MerchantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController("adminMerchantController")
@RequestMapping("/admin")
@Slf4j
public class MerchantController {

    @Autowired
    private DishService dishService;
    @Autowired
    private MerchantService merchantService;

    /**
     * 商户登录
     */
    @RequestMapping("/login")
    public Result login(@PathVariable MerchantDTO merchantDTO){
        log.info("商户登录");
        return merchantService.login(merchantDTO);
    }

    /**
     * 商户注册
     */
    @RequestMapping("/register")
    public Result register(@PathVariable Merchant merchant){
        log.info("商户注册");
        return merchantService.register( merchant);
    }

    /**
     * 商户信息修改（个人信息，密码。。。）
     */
    @RequestMapping("/update")
    public Result update(@PathVariable Merchant merchant){
        log.info("商户信息修改");
        return merchantService.updata( merchant);
    }

    /**
     * 全部订单查询
     */
    @RequestMapping("/order")
    public Result queryOrder(@PathVariable Integer page, @PathVariable Integer pageSize){
        log.info("订单查询");
        return merchantService.queryOrder(page, pageSize);
    }
    /**
     * 根据订单id查询订单
     */
    @RequestMapping("/order/{id}")
    public Result queryOrderById(@PathVariable Integer id){
        log.info("根据订单id查询订单");
        return merchantService.queryOrderById(id);
    }

    /**
     * 添加菜品
     */
    @RequestMapping("/dish")
    public Result addDish(@PathVariable("dish") Dish dish){
        log.info("添加菜品");
        if ((dish.getPrice() == null) ){
            return Result.error("未设置价格");
        }
        if (dish.getName() ==  null){
            return Result.error("未设置菜品名称");
        }
        dish.setStatus(1);
        dish.setCreatedAt(DateTime.now().toLocalDateTime());
        dish.setUpdatedAt(DateTime.now().toLocalDateTime());
        dishService.add(dish);
        return Result.success();
    }
}
